package cn.lingyangwl.agile.infra.job.task;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.lingyangwl.agile.auth.*;
import cn.lingyangwl.agile.common.core.config.*;
import cn.lingyangwl.agile.system.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

/**
 * @author shenguangyang
 */
@Slf4j
@Component
public class DeleteLogTask implements ITask {
    @Resource
    private GlobalProperties properties;
    @Resource
    private SystemApi systemApi;
    @Resource
    private AuthApi authApi;

    /**
     * 删除多少天之前的日志
     */
    public void exec() {
        String format = "yyyy-MM-dd HH:mm:ss";
        LocalDateTime endTime = LocalDateTime.now().plus(properties.getAccessLogReserveTime().toDays() * -1, ChronoUnit.DAYS);
        String endTimeStr = LocalDateTimeUtil.format(endTime, format);
        log.info("task-开始删除小于等于 {} 访问日志", endTimeStr);
        int count = systemApi.deleteAccessLog(endTimeStr);
        log.info("task-结束删除小于等于 {} 访问日志, count: {}", endTimeStr, count );

        log.info("task-开始删除小于等于 {} 登录日志", endTimeStr);
        count = authApi.deleteLoginLog(endTimeStr);
        log.info("task-结束删除小于等于 {} 登录日志, count: {}", endTimeStr, count );
    }
}
